<?php
namespace App\Services\Sale\Sale;

use App\Models\Sale\Sale;
use App\Models\Agent\Agent;
use App\Models\Sp\Sp;


class SaleLogic
{
    // 添加业务员
    public function addSale($data)
    {
        $data['create_at'] = date('Y-m-d H:i:s');
        $res = Sale::insert($data);
        return auto_return(['id' => $res],'添加成功！');
    }

    //营业员列表
    public function saleList(){
        $sale = Sale::paginate(10);
        return auto_return($sale,'成功！');
    }

    //更新，解绑，
    public function updateSale($data){
        if($data['status'] == 1){
            $res = Sale::where('id', '=', $data['id'])->update(['status'=>0]);
        }else{
            $res = Sale::where('id', '=', $data['id'])->update(['name'=>$data['name'],'email'=>$data['email'],'phone'=>$data['phone'],'type'=>$data['type'],'status'=>1]);
        }
        return auto_return(['id'=>$res],'成功!');
    }

    //离职
    public function unbind($data){
        $res = Agent::where('main', '=',$data['main'])->update(['main'=>$data['change_main']]);
        Sp::where('main', '=',$data['main'])->update(['main'=>$data['change_main']]);
        Sale::where('name', '=',$data['main'])->update(['status'=>2]);
        return auto_return(['id'=>$res],'成功!');
    }
   
}